/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.asportoonline.controller;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.asportoonline.business.beans.CategoriaManager;
import org.asportoonline.business.delegates.CategoriaManagerDelegate;
import java.util.List;
import org.asportoonline.entities.Categoria;

/**
 *
 * @author fe
 */
public class MainController extends HttpServlet {
    private CategoriaManager catm = new CategoriaManagerDelegate();

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        
        //per riempire la prima volta le categorie
        List<Categoria> lc = (List<Categoria>)catm.cercaTutteLeCategorie();
        if(lc == null) {
            inserisci_categorie();
        }         
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String s = request.getParameter("q").toString();

        if (s.equals("index")) {
            getServletContext().getRequestDispatcher("/LocaleAction?q=fetch_all_locality").forward(request, response);
        }
        else if (s.equals("index2")) {
            getServletContext().getRequestDispatcher("/index2.jsp").forward(request, response);
        }
        else if(s.equals("base_search")) {
            getServletContext().getRequestDispatcher("/LocaleAction?q=base_search").forward(request, response);
        }
        else if(s.equals("view_restaurants_by_locality")) {
            String loc = request.getParameter("loc").toString();
            getServletContext().getRequestDispatcher("/LocaleAction?q="+s+"&loc="+loc).forward(request, response);
        }
        else if(s.equals("view_restaurants_by_category")) {
            String cat = request.getParameter("cat").toString();
            String loc = request.getParameter("localita").toString();
            getServletContext().getRequestDispatcher("/LocaleAction?q="+s+"&cat="+cat+"&localita="+loc).forward(request, response);
        }
        else if(s.equals("view_results_search")) {
            getServletContext().getRequestDispatcher("/WEB-INF/views/view_results_search.jsp").forward(request, response);
        }
        else if (s.equals("login")) {
            getServletContext().getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
        } 
        else if (s.equals("login_action")) {
            getServletContext().getRequestDispatcher("/UtenteAction?q=login_action").forward(request, response);
        } 
        else if (s.equals("logout_action")) {
            getServletContext().getRequestDispatcher("/UtenteAction?q=logout_action").forward(request, response);
        } 
        else if (s.equals("register_user")) {
            getServletContext().getRequestDispatcher("/WEB-INF/views/register_user.jsp").forward(request, response);
        } 
        else if (s.equals("register_user_action")) {
            getServletContext().getRequestDispatcher("/UtenteAction?q=register_user_action").forward(request, response);
        } 
        else if (s.equals("fetch_user_data_action")) {
            getServletContext().getRequestDispatcher("/UtenteAction?q=fetch_user_data_action").forward(request, response);
        } 
        else if (s.equals("modify_user")) {
            getServletContext().getRequestDispatcher("/WEB-INF/views/modify_user.jsp").forward(request, response);
        } 
        else if (s.equals("modify_user_action")) {
            getServletContext().getRequestDispatcher("/UtenteAction?q=modify_user_action").forward(request, response);
        } 
        else if (s.equals("edit_password")) {
            getServletContext().getRequestDispatcher("/WEB-INF/views/edit_password.jsp").forward(request, response);
        } 
        else if (s.equals("change_password_action")) {
            getServletContext().getRequestDispatcher("/UtenteAction?q=change_password_action").forward(request, response);
        } 
        else if (s.equals("register_restaurant")) {
            getServletContext().getRequestDispatcher("/WEB-INF/views/register_restaurant.jsp").forward(request, response);
        } 
        else if (s.equals("register_restaurant_action")) {
            getServletContext().getRequestDispatcher("/LocaleAction?q=register_restaurant_action").forward(request, response);
        } 
        else if (s.equals("list_restaurants")) {
            getServletContext().getRequestDispatcher("/WEB-INF/views/list_restaurants.jsp").forward(request, response);
        } 
        else if (s.equals("restaurant_details")) {
            request.setAttribute("idlocale", request.getParameter("idlocale"));
            getServletContext().getRequestDispatcher("/WEB-INF/views/restaurant_details.jsp").forward(request, response);
        } 
        else if (s.equals("fetch_restaurants_data_action")) {  //recupera tutti i locali di un venditore
            String ret = request.getParameter("ret").toString();
            getServletContext().getRequestDispatcher("/LocaleAction?q=fetch_restaurants_data_action&AMP;ret="+ret).forward(request, response);
        } 
        else if (s.equals("fetch_restaurant_details_action")) {  //recupera tutti i dati di un certo locale tramite il suo id
            String idlocale = request.getParameter("idlocale").toString();
            String ret = request.getParameter("ret").toString();
            getServletContext().getRequestDispatcher("/LocaleAction?q=fetch_restaurant_details_action&ret="+ret+"&idlocale="+idlocale).forward(request, response);
        } 
        else if (s.equals("modify_restaurant")) {  //recupera tutti i dati di un certo locale tramite il suo id
            getServletContext().getRequestDispatcher("/WEB-INF/views/modify_restaurant.jsp").forward(request, response);
        } 
        else if (s.equals("modify_restaurant_action")) {  
            getServletContext().getRequestDispatcher("/LocaleAction?q=modify_restaurant_action").forward(request, response);
        }
        else if (s.equals("register_product")) { 
            String idlocale = request.getParameter("idlocale").toString();
            request.setAttribute("idlocale", idlocale);
            request.setAttribute("lista_categorie", catm.cercaTutteLeCategorie());
            getServletContext().getRequestDispatcher("/WEB-INF/views/register_product.jsp").forward(request, response);
        }
        else if(s.equals("register_product_action")) {
            getServletContext().getRequestDispatcher("/ProdottoAction?q=register_product_action").forward(request, response);
        }
        else if(s.equals("fetch_product_details_action")) {
            String idprodotto = request.getParameter("idprodotto").toString();
            String ret = request.getParameter("ret").toString();            
            getServletContext().getRequestDispatcher("/ProdottoAction?q=fetch_product_details_action&ret="+ret+"&idprodotto="+idprodotto).forward(request, response);
        }
        else if(s.equals("modify_product")) {
            getServletContext().getRequestDispatcher("/WEB-INF/views/modify_product.jsp").forward(request, response);
        }
        else if(s.equals("modify_product_action")) {
            getServletContext().getRequestDispatcher("/ProdottoAction?q="+s).forward(request, response);
        }
        else if(s.equals("prenotation_second_step")) {
            getServletContext().getRequestDispatcher("/PrenotazioneAction?q="+s).forward(request, response);
        }
        else if(s.equals("confirm_prenotation")) {
            getServletContext().getRequestDispatcher("/WEB-INF/views/confirm_prenotation.jsp").forward(request, response);
        }
        else if(s.equals("register_prenotation_action")) {
            getServletContext().getRequestDispatcher("/PrenotazioneAction?q="+s).forward(request, response);
        }
        else if(s.equals("fetch_user_prenotations_action")) {
            getServletContext().getRequestDispatcher("/PrenotazioneAction?q="+s).forward(request, response);
        }
        else if(s.equals("view_user_prenotations")) {
            getServletContext().getRequestDispatcher("/WEB-INF/views/view_user_prenotations.jsp").forward(request, response);
        }
        else if(s.equals("fetch_restaurant_prenotations_action")) {
            String idlocale = request.getParameter("idlocale").toString();
            getServletContext().getRequestDispatcher("/PrenotazioneAction?q="+s+"&idlocale="+idlocale).forward(request, response);
        }
        else if(s.equals("view_restaurant_prenotations")) {
            getServletContext().getRequestDispatcher("/WEB-INF/views/view_restaurant_prenotations.jsp").forward(request, response);
        }
        else if(s.equals("delete_restaurant_action")) {
            String idlocale = request.getParameter("idlocale").toString();
            getServletContext().getRequestDispatcher("/LocaleAction?q="+s+"&idlocale"+idlocale).forward(request, response);
        }
        else if(s.equals("delete_prenotation_action")) {
            String idprenotazione = request.getParameter("idprenotazione").toString();
            getServletContext().getRequestDispatcher("/PrenotazioneAction?q="+s+"&idprenotazione"+idprenotazione).forward(request, response);
        }
        
        else if(s.equals("user_prenotation_details_action")) {
            String idprenotazione = request.getParameter("idprenotazione").toString();
            getServletContext().getRequestDispatcher("/PrenotazioneAction?q="+s+"&idprenotazione"+idprenotazione).forward(request, response);
        }
        else if(s.equals("user_prenotation_details")) {
            getServletContext().getRequestDispatcher("/WEB-INF/views/user_prenotation_details.jsp").forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Main controller info";
    }// </editor-fold>

    private void inserisci_categorie() {
        Categoria cat = new Categoria();
        cat.setNome("Americano");
        catm.inserisciCategoria(cat);
        
        cat = new Categoria();
        cat.setNome("Arabo");
        catm.inserisciCategoria(cat);
        
        cat = new Categoria();
        cat.setNome("Biologico");
        catm.inserisciCategoria(cat);
        
        cat = new Categoria();
        cat.setNome("Carne");
        catm.inserisciCategoria(cat);
        
        cat = new Categoria();
        cat.setNome("Celiaco");
        catm.inserisciCategoria(cat);
        
        cat = new Categoria();
        cat.setNome("Cinese");
        catm.inserisciCategoria(cat);
        
        cat = new Categoria();
        cat.setNome("Crepes");
        catm.inserisciCategoria(cat);
        
        cat = new Categoria();
        cat.setNome("Gelato");
        catm.inserisciCategoria(cat);
        
        cat = new Categoria();
        cat.setNome("Giapponese");
        catm.inserisciCategoria(cat);
        
        cat = new Categoria();
        cat.setNome("Greco");
        catm.inserisciCategoria(cat);
            
        cat = new Categoria();
        cat.setNome("Indiano");
        catm.inserisciCategoria(cat);
            
        cat = new Categoria();
        cat.setNome("Italiano");
        catm.inserisciCategoria(cat);
            
        cat = new Categoria();
        cat.setNome("Kebab");
        catm.inserisciCategoria(cat);
            
        cat = new Categoria();
        cat.setNome("Messicano");
        catm.inserisciCategoria(cat);
            
        cat = new Categoria();
        cat.setNome("Panini");
        catm.inserisciCategoria(cat);
            
        cat = new Categoria();
        cat.setNome("Pesce");
        catm.inserisciCategoria(cat);
            
        cat = new Categoria();
        cat.setNome("Piadina");
        catm.inserisciCategoria(cat);
            
        cat = new Categoria();
        cat.setNome("Thailandese");
        catm.inserisciCategoria(cat);
            
        cat = new Categoria();
        cat.setNome("Vegetariano");
        catm.inserisciCategoria(cat);
            
        cat = new Categoria();
        cat.setNome("Pizza");
        catm.inserisciCategoria(cat);
            
        cat = new Categoria();
        cat.setNome("Spagnolo");
        catm.inserisciCategoria(cat);

        cat = new Categoria();
        cat.setNome("Aperitivo");
        catm.inserisciCategoria(cat);
        
        cat = new Categoria();
        cat.setNome("Etnico");
        catm.inserisciCategoria(cat);
    }
}
